package miku.firstgame.model
{
	import miku.firstgame.model.gold.card.ToolType;
	import miku.firstgame.model.proxy.ProxyHelper;
	import miku.firstgame.net.ByteArrayEx;

	public class User
	{
		public static const STATUS_UNKNOWN:int = 1;
		public static const STATUS_LOBBY:int = 2;
		public static const STATUS_ROOM:int = 4;
		public static const STATUS_GAME:int = 8;
		public static const STATUS_ALL:int = STATUS_UNKNOWN | STATUS_LOBBY
			| STATUS_ROOM | STATUS_GAME;

		public var id:int;

		public var name:String;

		public var status:int = 1; //暂时不考虑用户状态的更新

		private var _isReady:Boolean = false;

		/**
		 * 下面这些需要根据gold game提出来
		 * */

		private var lackTools:int = 0;

		/**
		 * 是否能够行动
		 * */
		public function getCanAction():Boolean
		{
			if (lackTools == 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/**
		 * 移除该玩家身上的工具
		 *
		 * @param type
		 *            ToolType
		 * @return
		 */
		public function removeTools(type:int):void
		{
			lackTools = lackTools | type;
		}

		/**
		 * 得到一个工具
		 * */
		public function getTool(type:int):void
		{
			lackTools = ((~type) & ToolType.ALL) & lackTools;
		}

		/**
		 * 是否有工具
		 * */
		public function hasTool(type:int):Boolean
		{
			if ((type & lackTools) == type) // 说明缺少该类型
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		public function get isReady():Boolean
		{
			return _isReady;
		}

		public function set isReady(value:Boolean):void
		{
			_isReady = value;
		}

		public function User(id:int)
		{
			this.id = id;
		}

		public static function parseUser(data:ByteArrayEx):User
		{
			var id:int = data.readUnsignedInt();
			var status:int = data.readUnsignedShort();
			var name:String = data.readString();
			var user:User = ProxyHelper.getUser(id);
			if (user == null)
			{
				if (id == Me.Instace.id)
				{
					user = Me.Instace;
				}
				else
				{
					user = new User(id);
				}
			}
			user.status = status;
			user.name = name;
			return user;
		}
	}
}
